// pages/bindMobile/bindMobile.js
let gradeModel = require('../../models/grade.js')
let klassModel = require('../../models/klass.js')
let request = require('../../operation/operation.js')
let schoolModel = require('../../models/school.js')

let phoneNumber = ''

Page({

  /**
   * 页面的初始数据
   */
  data: {
    region:null,
    grade:null,
    klass:null,
    // school:null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      // region: getApp().globalData.param.region,
      grade: gradeModel.getGradeByEnrolDate(getApp().globalData.param.enrolDate),
      klass: klassModel.getClassByIndex(getApp().globalData.param.class),
      // school: getApp().globalData.param.school
      schoolName: getApp().globalData.param.schoolName,
      klassName: getApp().globalData.param.klassName,
      category: getApp().globalData.param.category
    })
  },

  /**
   * 获取微信绑定手机号的回调处理函数
   */
  getPhoneNumber:function(event){
    let that = this
    if (event.detail.encryptedData) {
      request.postRequest('/get-phone-number', { 'encryptedData': event.detail.encryptedData, 'iv': event.detail.iv}, true)
        .then(res => {
          var data = res.data;
          if (request.SUCCESSED == data.status) {
            phoneNumber = data.object.phoneNumber
            that.createClass(phoneNumber,false)
          } else {
            wx.showToast({
              title: data.msg,
              icon: 'none'
            })
          }

        }).catch(e => {
          console.log(e)
        })
    }
  },

  createClass: function (phoneNumber, testOption = false) {
    let that = this

    // let param = { 'schoolSid': getApp().globalData.param.school.sid, 'enrollDate': getApp().globalData.param.enrolDate + '-09-01', 'index': parseInt(getApp().globalData.param.class) + 1, 'phoneNumber': phoneNumber};
    let param = { 'category': getApp().globalData.param.category,'schoolName':getApp().globalData.param.schoolName, 'phoneNumber': phoneNumber };
    if (schoolModel.CATEGORY_PRIMARY_SCHOOL == getApp().globalData.param.category) {
      param.enrollDate = getApp().globalData.param.enrolDate + '-09-01'
      param.index = parseInt(getApp().globalData.param.class) + 1
    }else {
      param.name = getApp().globalData.param.klassName
    }
    if (testOption) {
      param.testOption = 'exist'
    }
    
    request.postRequest('/v1/klasses', param, true)
      .then(res => {
        var data = res.data;
        if (request.SUCCESSED == data.status) {
          var klass = data.object;
          // 将新创建的班级自动设置成当前班级，将班级添加进所关联所有班级操作由“我的”界面中，更新用户信息接口去完成
          // klassModel.setCurrentClass(klass);
          getApp().globalData.param = klass
          wx.reLaunch({
            url: '../classFinished/classFinished',
          })
        } else {
          if (-6 == data.status) {
            wx.showModal({
              title: '提示',
              content: data.msg + '，是否继续创建？',
              showCancel: true,
              cancelText: '返回',
              confirmText: '继续',
              success: function(res) {
                if (res.confirm) {
                  that.createClass(phoneNumber, false)
                }else if (res.cancel) {
                  wx.navigateBack({
                    delta: 1,
                  })
                }
              }
            })
          }else {
            wx.showToast({
              title: data.msg,
              icon: 'none'
            })
          }          
        }

      }).catch(e => {
        console.log(e)
      })
  }
})